﻿using LeadChina.NETFramework.Common;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace LeadChina.AssemblyLine.Common.Serial
{

    /// <summary>
    /// 成都普瑞逊称重模块 TC02
    /// </summary>
    public class WeighingModuleCom
    {
        private readonly SerialPort serialPort;

        public EnumEncoding EnumEncoding { get; set; } = EnumEncoding.ASCII;

        public event Action<int> DataRecvEvent;
        private int readDelay = 100;
        private byte[] sendBytes = new byte[] { 0xFF, 0x03, 0x00, 0x00, 0x00, 0x02, 0xD1, 0xD5 };//重量问询帧
        private byte[] recvBytes = new byte[9];

        ~WeighingModuleCom()
        {
            try
            {
                serialPort.Close();
            }
            catch (Exception ex)
            {

            }
        }


        public WeighingModuleCom(string portName, int baudRate, Parity parity, int databits, StopBits stopBits)
        {
            serialPort = new SerialPort(portName, baudRate, parity, databits, stopBits);
            serialPort.DataReceived += Com_DataReceived;
            if (serialPort.IsOpen)
            {
                serialPort.Close();
            }
            serialPort.Open();
        }
        public WeighingModuleCom(string portName) : this(portName, 9600, Parity.None, 8, StopBits.One) { }
        public WeighingModuleCom(string portName, int baudRate) : this(portName, baudRate, Parity.None, 8, StopBits.One) { }
        public WeighingModuleCom(string portName, int baudRate, Parity parity) : this(portName, baudRate, parity, 8, StopBits.One) { }
        public WeighingModuleCom(string portName, int baudRate, Parity parity, int databits) : this(portName, baudRate, parity, databits, StopBits.One) { }


        /// <summary>
        /// 串口接收事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Com_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            Thread.Sleep(readDelay);
            try
            {
                serialPort.Read(recvBytes, 0, recvBytes.Length);
                byte[] crcData = recvBytes.Take(7).ToArray();
                byte[] crcCode = CRC.CRC16(crcData, crcData.Length);
                if (crcCode[0] == recvBytes[7] && crcCode[1] == recvBytes[8])
                {
                    byte[] weightBytes = { recvBytes[4], recvBytes[3], recvBytes[6], recvBytes[5] };
                    int weight = BitConverter.ToInt32(weightBytes, 0);
                    DataRecvEvent?.Invoke(weight);
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("称重接收异常：", ex);
            }
          
        }


        /// <summary>
        /// 指令发送
        /// </summary>
        /// <param name="bytes"></param>
        public void SendCommand()
        {
            serialPort.Write(sendBytes, 0, sendBytes.Length);
        }

    }
}
